package ConveyorBelt.State;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import ConveyorBelt.Base.Score;
import ConveyorBelt.Util.FontUtil;
import ConveyorBelt.Util.ImageUtil;

public class LevelWin extends BasicGameState {
	public static int stateID = 5;
	private int winX, winY;
	private int	continueGameButtonX, continueGameButtonY;
	private int backButtonX, backButtonY;
	private int mouseX, mouseY;
	private float continueGameScale = 1;
	private float backScale = 1;
	private float scaleStep = 0.0001f;
	
	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		ImageUtil.loadAllImage();
		FontUtil.loadGameFont();

		winX = gc.getWidth()/2 - ImageUtil.win.getWidth()/2;
		winY = gc.getHeight()/2 - ImageUtil.win.getHeight()/2 - 200;	
		continueGameButtonX = winX - 100;
		continueGameButtonY = winY + 500;	
		backButtonX = winX + 100 + ImageUtil.win.getWidth() - ImageUtil.backButton.getWidth();
		backButtonY = winY + 500;
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		ImageUtil.background.draw(0, 0);
		ImageUtil.win.draw(winX, winY);
		ImageUtil.continueGameButton.draw(continueGameButtonX, continueGameButtonY, continueGameScale);
		ImageUtil.backButton.draw(backButtonX, backButtonY, backScale);
		g.setFont(FontUtil.uf);
		g.drawString("Your score:     "+Score.getCurrentScore(),winX, winY+300);
 		g.drawString("OOP 2D Game Project:      Can You Help Me???      - Author: Phu - Manh - Quang", 200, 650);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		Input input = gc.getInput();
		mouseX = input.getMouseX();
		mouseY = input.getMouseY();
		boolean insideContinueGame = false;
		boolean insideBack = false;
		int tmpX, tmpY;

		if((mouseX >= continueGameButtonX && mouseX <= continueGameButtonX + ImageUtil.continueGameButton.getWidth()) && 
				(mouseY >= continueGameButtonY && mouseY <= continueGameButtonY + ImageUtil.continueGameButton.getHeight())) {
			insideContinueGame = true;
		}
		tmpY =  gc.getHeight()/2 - ImageUtil.win.getHeight()/2 - 200 + 500;	
		if(insideContinueGame) {
			tmpX = winX - 100 + 10;
			if(continueGameScale < 1.05f) continueGameScale += scaleStep*delta;
			if(continueGameButtonX < tmpX) continueGameButtonX ++;
			if(continueGameButtonY >= tmpY) continueGameButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
				sbg.getState(Game.stateID).init(gc, sbg);
				sbg.enterState(Game.stateID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
			}
		}
		else {
			tmpX = winX - 100;
			if(continueGameScale > 1.0f) continueGameScale -= scaleStep*delta;
			if(continueGameButtonX > tmpX) continueGameButtonX--;
			if(continueGameButtonY <= tmpY) continueGameButtonY++;
		}
		
		if((mouseX >= backButtonX && mouseX <= backButtonX + ImageUtil.backButton.getWidth()) && 
				(mouseY >= backButtonY && mouseY <= backButtonY + ImageUtil.backButton.getHeight())) {
			insideBack = true;
		}
		tmpY = gc.getHeight()/2 - ImageUtil.win.getHeight()/2 - 200 + 500;
		if(insideBack) {
			tmpX = winX + 100 + ImageUtil.win.getWidth() - ImageUtil.backButton.getWidth() + 10;
			if(backScale < 1.05f) backScale += scaleStep*delta;
			if(backButtonX < tmpX) backButtonX ++;
			if(backButtonY >= tmpY) backButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
				Game.levelNumber = 1;
				sbg.getState(MainMenu.stateID).init(gc, sbg);
				sbg.enterState(MainMenu.stateID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
			}
		}
		else {
			tmpX = winX + 100 + ImageUtil.win.getWidth() - ImageUtil.backButton.getWidth();
			if(backScale > 1.0f) backScale -= scaleStep*delta;
			if(backButtonX > tmpX) backButtonX--;
			if(backButtonY <= tmpY) backButtonY++;
		}
	}

	@Override
	public int getID() {
		return stateID;
	}

}
